﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Linq;
using System.Xml.Linq;

namespace GrooveSharp
{
    static class Exporter
    {
        private static string Plural(string name)
        {
            return name + "s";
        }

        private static string OptionalTimeSpan(TimeSpan? span)
        {
            return span.HasValue ? span.ToString() : "null";
        }

        public static void Export(string fileName)
        {
            // The oneline XML composing and saving function
            // (see also http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html )
            new XDocument
                (
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment(String.Format("Groove# Data, exported at {0} by {1}", DateTime.Now, Db.UserName)),
                    new XElement
                        (
                            ImpExp.XML_ELEMENT_MAIN,
                            new XElement
                                (
                                    Plural(ImpExp.XML_ELEMENT_PLAY),
                                    from item in ImpExp.Plays()
                                    select new XElement
                                        (
                                            ImpExp.XML_ELEMENT_PLAY,
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FOLDERNAME, item.FolderName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FILENAME, item.FileName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_DATE, item.PlayDate),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_DURATION, OptionalTimeSpan(item.Duration))
                                        )
                                ),
                            new XElement
                                (
                                    Plural(ImpExp.XML_ELEMENT_RATING),
                                    from item in ImpExp.Ratings()
                                    select new XElement
                                        (
                                            ImpExp.XML_ELEMENT_RATING,
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FOLDERNAME, item.FolderName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FILENAME, item.FileName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_RATING, item.RatingValue),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_DATE, item.PlayDate)
                                        )
                                ),
                            new XElement
                                (
                                    Plural(ImpExp.XML_ELEMENT_FLAG),
                                    from item in ImpExp.Flags()
                                    select new XElement
                                        (
                                            ImpExp.XML_ELEMENT_FLAG,
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FOLDERNAME, item.FolderName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FILENAME, item.FileName),
                                            new XAttribute(ImpExp.XML_ATTRIBUTE_FLAG, item.FlagName)
                                        )
                                )
                        )
                ).Save(fileName);
        }

    }
}
